package cn.edu.gdgm.system.controller;

import cn.edu.gdgm.system.domain.WeChatProfile;
import cn.edu.gdgm.system.dto.RestResult;
import cn.edu.gdgm.system.exception.WeChatServiceException;
import cn.edu.gdgm.system.service.IWechatUserinfoService;
import cn.fightingguys.security.web.wechat.entity.WeChatMiniProgramUserDetails;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("user")
public class UserProfileController {

    private IWechatUserinfoService wechatUserinfoService;

    @PostMapping("profile")
    @PreAuthorize("hasAuthority('Verified')")
    public RestResult<Void> updateProfile(@AuthenticationPrincipal WeChatMiniProgramUserDetails userDetails, WeChatProfile weChatProfile) throws WeChatServiceException {
        wechatUserinfoService.updateProfile(userDetails, weChatProfile);
        return RestResult.ok();
    }

    @Autowired
    public UserProfileController setWechatUserinfoService(IWechatUserinfoService wechatUserinfoService) {
        this.wechatUserinfoService = wechatUserinfoService;
        return this;
    }

}
